import click
from app import db
from app.models import User, Movie  # 确保从正确的路径导入模型


def forge_command():
    """生成测试数据的核心逻辑：清空表→重建表→插入测试数据"""
    db.drop_all()
    db.create_all()

    # 创建测试用户（严格按照 User 模型的字段来提供参数）
    # 你的模型定义了 username, email, password 三个非空字段
    test_user = User(
        username="周佳伟",  # 改为周佳伟
        email="zhangsan@example.com",
        password="123456"  # 密码保持123456
    ) #
    # 创建测试电影数据
    test_movies = [
        Movie(title="The Shawshank Redemption", year=1994),
        Movie(title="The Godfather", year=1972),
        Movie(title="The Dark Knight", year=2008),
        Movie(title="12 Angry Men", year=1957),
        Movie(title="Schindler's List", year=1993),
    ]

    # 提交数据到数据库
    db.session.add(test_user)
    db.session.add_all(test_movies)
    db.session.commit()

    click.echo("Fake test data generated successfully!")
    click.echo(f"测试用户 '张三' 创建成功，密码：123456")


def register_forge(app):
    """将 forge 命令注册到 Flask 应用"""

    @app.cli.command("forge")
    def forge():
        """Generate fake test data for the database."""
        forge_command()